package com.etc.dao.impl;

import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.etc.dao.UserDao;
import com.etc.entity.User;
import com.etc.test.UserRowMapper;
@Repository
public class UserDaoImpl implements UserDao {

	private ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
	private JdbcTemplate jdbcTemplate=context.getBean("jdbcTemplate",JdbcTemplate.class);
	/**
	 * 查询总记录数
	 */
	@Override
	public int selectCount() {
		String sql ="select count(*) from bg_user";
		int n = jdbcTemplate.queryForObject(sql, Integer.class);
		return n;
	}
	/**
	 * 根据Id查询用户信息
	 */
	@Override
	public User getUser(int id) {
		String sql ="select * from bg_user where id=?";
		User user=jdbcTemplate.queryForObject(sql, new UserRowMapper(), id);
		return user;
	}
	/**
	 * 查询所有用户信息
	 */
	@Override
	public List<User> getAllUser() {
		List<User> list =jdbcTemplate.query("select * from bg_user",new UserRowMapper());
		return list;
	}
	/**
	 * 新增用户
	 */
	@Override
	public int addUser(User user) {
		int n = jdbcTemplate.update("insert into bg_user(name,pwd) values(?,?)", user.getName(),user.getPwd());
		return n;
	}
	/**
	 * 根据Id删除用户
	 */
	@Override
	public int deleteUser(int id) {
		int n = jdbcTemplate.update("delete from bg_user where id = ?",id);
		return n;
	}
	/**
	 * 修改用户信息
	 */
	@Override
	public int updateUser(User user) {
		int n = jdbcTemplate.update("update bg_user set name=?,pwd=? where id=?", user.getName(),user.getPwd(),user.getId());
		return n;
	}

}
